import { pdf } from "pdf-to-img";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import fs from 'fs'

const __dirname = dirname(fileURLToPath(import.meta.url));

export const parse = async (file: string) => {
  const document = await pdf(file, { scale: 3 });
  const images = [];
  for await (const image of document) {
    images.push(image);
  }
  return {
    file,
    images,
  };
};

const pdfs = await parse(
  `/Volumes/Data/POC测试集/合同POC/ANHUI\ BASHI-YUEXIN\ INTERNATIONAL\ LOGISTICS\ CO.\,LTD_VC202311290024HL_SIGNED\ VERSION_20231130172542.pdf`
);

let page = 1

for await (const image of pdfs?.images) {
    await fs.promises.writeFile(join(__dirname, '../../images', `page${page}.png`), image);
    page++;
}
